/**
 * 11选5的共用逻辑
 */
(function() {
    var currTabId = 1;
    var tabDraws = [
        // 1：两面
        fillLM,
        
        // 1-5球
        function() { fill_table(1); },
        function() { fill_table(2); },
        function() { fill_table(3); },
        function() { fill_table(4); },
        function() { fill_table(5); },

        // 连码
        fill_lianma,
    ];
    
    function fillLM() {
        var types = ["大", '小', "单", '双', "尾大", '尾小', "龙", '虎'];
        var div = $(".zonghe");
        div.empty();
        for (var i = 0; i < types.length; i++) {
            var key = types[i];
            var name = types[i];
            if (key == '大' || key == '小' || key == '单' || key == '双') {
                key = '和' + key;
                name = '总和' + name;
            }
            if (key == '尾大' || key == '尾小') name = '和' + name;
            add_bet_item(key, name, div);
        }

        var arr = ['一', '二', '三', '四', '五'];
        var types = ["大", "小", "单", "双"]; 
        var div = $(".five");
        div.empty();
        for (var i in arr) {
            add_bet_th("第" + arr[i] + "球", div);
            var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
            for (var j = 0; j < types.length; j++) {
                add_bet_item(arr[i] + types[j], types[j], ul);
            }
            div.append(ul);
        }
    }

    function fill_table(index) {
        var arr = ['一', '二', '三', '四', '五'];
        var types = ["大", "小", "单", "双"]; 
        var i = index - 1;
        {
            var div = $(".q" + (i + 1));
            div.empty();

            // 1-9
            var ul = $('<ul class="am-avg-sm-3 bet"></ul>');
            for (var j = 1; j <= 9; j++) {
                var k = j < 10 ? "0" + j : j;
                var t = '<div class="bet-content round-1">' + j + '</div>';
                add_bet_item(arr[i] + k, t, ul);
            }
            div.append(ul);

            var ul = $('<ul class="am-avg-sm-2 bet"></ul>');
            add_bet_item(arr[i] + "10", '<div class="bet-content round-1">' + 10 + '</div>', ul);
            add_bet_item(arr[i] + "11", '<div class="bet-content round-1">' + 11 + '</div>', ul);
            for (var j = 0; j < types.length; j++) {
                add_bet_item(arr[i] + types[j], types[j], ul);
            }
            div.append(ul);
        }
    }

    function fill_lianma()
    {
        var ul = $(".page7 .n1-9"); ul.empty();
        for (var i = 1; i <= 9; i++) {
            ul.append('<li class="bet-item"><div class="bet-content round-1">' + i + '</div></li>');
        }
        var ul = $(".page7 .n10-11"); ul.empty();
        for (var i = 10; i <= 11; i++) {
            ul.append('<li class="bet-item"><div class="bet-content round-1">' + i + '</div></li>');
        }

        var _drawBanner = function() {
            var type = $(".page7 .types .active").attr("value");
            $(".page7 .peilv span").attr("key", type);
            $(".page7 li").removeClass("bet-active");

            // 设置赔率
            var rate = gameRule.map[type] ? gameRule.map[type].rate : "";
            $(".page7 .peilv span").html(rate);
        };
        $(".page7 .types li").click(function(e) {
            $(".page7 .types li").removeClass("active");
            $(e.currentTarget).addClass("active");
            _drawBanner();
        });
    }

    // 需要自己处理数字被点击
    var clickBetItemF = function(target) {
        var n = target.find("bet-content").html();
        if ($(".page7").css("display") == 'none') return;
        if (target.attr("class") && target.attr("class").indexOf("bet-active") != -1) return;

        var item = $(".page7 .types .active");
        var min = parseInt(item.attr("min")), max = parseInt(item.attr("max"));
        if ($(".page7 .bet-active").length >= max) {
            alert2("不允许超过" + max + "个选项");
            return true;
        }

        // 可以选中，走通用逻辑
        return;
    }

    // 取得注单列表
    var extraListF = function(data) {
        if ($(".page7").css("display") == 'none') return data;

        var item = $(".page7 .types .active");
        var min = parseInt(item.attr("min"));
        var arr = $(".page7 .bet-active .bet-content");
        if (arr.length < min) return data;

        var k = item.attr("value");
        var numbers = [];
        for (var i = 0; i < arr.length; i++) {
            numbers.push($(arr[i]).html());
        }

        var v = $(".footer input").val().trim();
        v = parseInt(v);
        data[k + ":" + numbers.join(",")] = v * 100;
        return data;
    }

    var parseTargetF = function(k) {
        if (k.startsWith('组2')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = 1;
            if (numbers.length == 3) i = 3;
            if (numbers.length == 4) i = 6;
            if (numbers.length == 5) i = 10;
            return '前二组选 ' + arr[1] + ' [' + i + '组]';
        }
        if (k.startsWith('组3')) {
            var arr = k.split(':');
            var numbers = arr[1].split(',');
            var i = 1;
            if (numbers.length == 4) i = 4;
            if (numbers.length == 5) i = 10;
            return '前三组选 ' + arr[1] + ' [' + i + '组]';
        }
        return gameRule.getTargetName(k);
    }

    // 需要自己处理下注的逻辑
    var calcBetListF = function(data) {
        if ($(".page7").css("display") == 'none') return null;
        var total = 0, count = 0;
        for (var k in data) {
            if (k.startsWith('组2')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var i = 1;
                if (numbers.length == 3) i = 3;
                if (numbers.length == 4) i = 6;
                if (numbers.length == 5) i = 10;
                count += i;
                total += data[k] * i;
                continue;
            }
            if (k.startsWith('组3')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var i = 1;
                if (numbers.length == 4) i = 4;
                if (numbers.length == 5) i = 10;
                count += i;
                total += data[k] * i;
                continue;
            }
            count++;
            total += data[k];
        }
        return [count, total/100];
    }

    var unpackF = function(data) {
        if ($(".page7").css("display") == 'none') return data;

        var r = {};
        for (var k in data) {
            if (k.startsWith('组2')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group2(numbers);
                for (var i in list) {
                    r['组2:' + list[i].join(',')] = data[k];
                }
                r.group = '前二组选 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else if (k.startsWith('组3')) {
                var arr = k.split(':');
                var numbers = arr[1].split(',');
                var list = group3(numbers);
                for (var i in list) {
                    r['组3:' + list[i].join(',')] = data[k];
                }
                r.group = '前三组选 ' + arr[1] + '[' + list.length + '组]_' + get_server_time();
            }
            else
                r[k] = data[k];
        }
        return r;
    }

    var drawF2 = function() {
        var c = Result.last();
        if (!c || !c.red) return [];
        var red = c.red;
        var r = [];

        // 冠亚军和
        var he = gameRule.he(red);
        r.push(he);

        if (gameRule.isHeBig(red)) r.push("大");
        else if (gameRule.isHeSmall(red)) r.push("小");

        if (gameRule.isHeOdd(red)) r.push("单");
        else r.push("双");

        if (gameRule.isHeWeiBig(red)) r.push("尾大");
        else r.push("尾小");

        if (gameRule.isLong(red))
            r.push("龙");
        else if (gameRule.isHu(red))
            r.push("虎");
        else if (gameRule.isHe(red))
            r.push("和");

        return r;
    }

    var drawF = function(i) {
        i = parseInt(i);
        return '<span class="round">' + i + '</span>';
    }

    // 检测能否下注，看下单号1-10是否满足要求
    var checkF = function(type, issue, data) {
        var limit = Util.getNumberLimit(type);
        if (limit <= 0 || limit >= 10) return true;

        var arr1 = ['一', '二', '三', '四', '五'];
        var arr2 = ['01', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11'];
        var m = {};
        for (var k in data) {
            if (k.length != 3) continue;
            var k1 = k[0], k2 = k.substring(1);
            if (arr1.indexOf(k1) != -1 && arr2.indexOf(k2) != -1) {
                if (!m[k1]) m[k1] = {};
                m[k1][k2] = true;
            }
        }
        if (Util.isEmpty(m)) return true;

        // 获取下已经下注的单据
        var bets = context.bets[type];
        if (bets) bets = bets[issue] || [];
        else bets = [];
        for (var i in bets) {
            var k = bets[i].target;
            if (k.length != 3) continue;
            var k1 = k[0], k2 = k.substring(1);
            if (arr1.indexOf(k1) != -1 && arr2.indexOf(k2) != -1) {
                if (!m[k1]) m[k1] = {};
                m[k1][k2] = true;
            }
        }

        var total = 0;

        for (var k in m) {
            var m2 = m[k];
            if (Object.keys(m2).length > limit) {
                var l = "下注失败，__g__ 定位玩法，单个名次不允许下注__v__码或者__v__以上！如有疑问，请咨询客服！";
                l = l.replace(/__g__/, get_game_display_name(type));
                l = l.replace(/__v__/g, limit);
                alert2(l, true);
                return false;
            }
        }

        return true;
    }

    // 滚动
    var switchTabF = function() {
        new IScroll(".page7 .banner", {
            scrollX: true,
            scrollY: false,
            click: true,
        });
    };

    var initF = function(game, first) {
        window.game = game;
        initGame(game, function() {
            if (first !== false) fill_tabs(['两面', '第一球', '第二球', '第三球', '第四球', '第五球', '连码']);
            currTabId = parseInt($(".menu-left a.active").attr("value"));
            $(".page" + currTabId).attr("flag", 1);
            var f = tabDraws[currTabId - 1];
            f();
        }, first);
    }

    var data = {
        drawResult: drawF,
        drawResult2: drawF2,
        init: initF,
        checkBetList: checkF,
        switchTab: switchTabF,
        clickBetItem: clickBetItemF,
        extraList: extraListF,
        parseTarget: parseTargetF,
        calcBetList: calcBetListF,
        unpack: unpackF,
    }
    context.global.game["gd115"] = data;
    context.global.game["zy115"] = data;
})();